# -*- coding: utf-8 -*-
#
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

"""
名称映射配置

提供Python名称到C++名称的映射。
"""

from typing import Dict

# Python名称到C++名称的映射
NAME_MAPPINGS: Dict[str, str] = {
    # 特殊标识符
    "self": "this",
    # 常量值
    "None": "std::nullopt",
    "True": "true",
    "False": "false",
    # 注意：内置类型和函数的映射已移除，现在通过 builtin 配置系统处理
}

# 需要特殊处理的名称（不能简单映射）
# 注意：内置函数的特殊处理已移除，现在通过 builtin 配置系统和发射器处理
SPECIAL_NAMES = set()


def get_name_mapping(python_name: str) -> str:
    """获取Python名称对应的C++名称

    Args:
        python_name: Python名称

    Returns:
        str: 对应的C++名称
    """
    return NAME_MAPPINGS.get(python_name, python_name)


def is_special_name(python_name: str) -> bool:
    """检查是否为需要特殊处理的名称

    Args:
        python_name: Python名称

    Returns:
        bool: 是否需要特殊处理
    """
    return python_name in SPECIAL_NAMES


def is_python_keyword(name: str) -> bool:
    """检查是否为Python关键字

    Args:
        name: 名称

    Returns:
        bool: 是否为Python关键字
    """
    python_keywords = {
        "and",
        "as",
        "assert",
        "break",
        "class",
        "continue",
        "def",
        "del",
        "elif",
        "else",
        "except",
        "exec",
        "finally",
        "for",
        "from",
        "global",
        "if",
        "import",
        "in",
        "is",
        "lambda",
        "not",
        "or",
        "pass",
        "print",
        "raise",
        "return",
        "try",
        "while",
        "with",
        "yield",
        "None",
        "True",
        "False",
    }
    return name in python_keywords


def is_cpp_keyword(name: str) -> bool:
    """检查是否为C++关键字

    Args:
        name: 名称

    Returns:
        bool: 是否为C++关键字
    """
    cpp_keywords = {
        "alignas",
        "alignof",
        "and",
        "and_eq",
        "asm",
        "auto",
        "bitand",
        "bitor",
        "bool",
        "break",
        "case",
        "catch",
        "char",
        "char16_t",
        "char32_t",
        "class",
        "compl",
        "const",
        "constexpr",
        "const_cast",
        "continue",
        "decltype",
        "default",
        "delete",
        "do",
        "double",
        "dynamic_cast",
        "else",
        "enum",
        "explicit",
        "export",
        "extern",
        "false",
        "float",
        "for",
        "friend",
        "goto",
        "if",
        "inline",
        "int",
        "long",
        "mutable",
        "namespace",
        "new",
        "noexcept",
        "not",
        "not_eq",
        "nullptr",
        "operator",
        "or",
        "or_eq",
        "private",
        "protected",
        "public",
        "register",
        "reinterpret_cast",
        "return",
        "short",
        "signed",
        "sizeof",
        "static",
        "static_assert",
        "static_cast",
        "struct",
        "switch",
        "template",
        "this",
        "thread_local",
        "throw",
        "true",
        "try",
        "typedef",
        "typeid",
        "typename",
        "union",
        "unsigned",
        "using",
        "virtual",
        "void",
        "volatile",
        "wchar_t",
        "while",
        "xor",
        "xor_eq",
    }
    return name in cpp_keywords


def escape_cpp_name(name: str) -> str:
    """转义C++名称，避免关键字冲突

    Args:
        name: 原始名称

    Returns:
        str: 转义后的名称
    """
    if is_cpp_keyword(name):
        return f"{name}_"  # 添加下划线后缀
    return name
